package com.moody.web;


/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-9-2 下午01:30:32
 * @Modified by: 
 * @Description: 控制左侧导航树的跳转
 */


import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.constant.DispatchTypeConstant;
import com.moody.hibernate.dao.CustAddressDAO;
import com.moody.hibernate.dao.CustInfoDAO;
import com.moody.hibernate.dao.CustOperationDAO;
import com.moody.hibernate.domain.CustInfo;
import com.moody.service.DispatchService;

@Controller
@RequestMapping("/")
public class DispatchController {
	private static final Logger log = LoggerFactory.getLogger(DispatchController.class);
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	private CustAddressDAO custAddressDao = CustAddressDAO.getFromApplicationContext(ctx);
	private CustInfoDAO custInfoDao = CustInfoDAO.getFromApplicationContext(ctx);
	private CustOperationDAO custOperationDao = CustOperationDAO.getFromApplicationContext(ctx);
	
	@Resource
	private DispatchService dispatchService;
	
	@RequestMapping("/dispatch")
	public String dispatch(String custId, String type, Model model){
		String forward = "";
		if(type==null || type.equals("")){
			forward = "error";
			log.info("dipatch error");
		}else{
			//客户信息
			if(type.toLowerCase().equals(DispatchTypeConstant.CUST_GENERAL.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_General";
				}
				else{
					if(dispatchService.getCustomerInfo(custId) == null){
						forward = "customer/AddCustomer_General";
					}else{
						forward = "customer/CheckCustomer_General";
						model.addAttribute("custInfo",dispatchService.getCustomerInfo(custId));
					}	
				}	
			}
			//客户地址信息
			else if(type.toLowerCase().equals(DispatchTypeConstant.CUST_ADDRESS.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_Address";
				}
				else{
					if(dispatchService.getCustomerAddress(custId) == null || dispatchService.getCustomerAddress(custId).size()==0){
						forward = "customer/AddCustomer_Address";
					}else{
						forward = "customer/CheckCustomer_Address";
						model.addAttribute("custAddressList",dispatchService.getCustomerAddress(custId));
					}	
				}				
			}
			//客户经营信息
			else if(type.toLowerCase().equals(DispatchTypeConstant.CUST_OPERATION.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_Operation";
				}
				else{
					if(dispatchService.getCustomerOperation(custId) == null ){
						forward = "customer/AddCustomer_Operation";
					}else{
						model.addAttribute("custInfo",dispatchService.getCustomerOperation(custId));
						forward = "customer/CheckCustomer_Operation";
					}	
				}	
			}
			//高管信息
			else if(type.toLowerCase().equals(DispatchTypeConstant.CUST_MANAGEMENT.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_Management";
				}
				else{
					if(dispatchService.getCustomerManager(custId) == null || dispatchService.getCustomerManager(custId).size()==0){
						forward = "customer/AddCustomer_Management";
					}else{
						model.addAttribute("custManagerList",dispatchService.getCustomerManager(custId));
						forward = "customer/CheckCustomer_Management";
					}	
				}
			}
			
			//概览
			else if(type.toLowerCase().equals(DispatchTypeConstant.CUST_OVERVIEW.toLowerCase())){
				CustInfo custInfo = dispatchService.getCustomerInfo(custId);
				custInfo.setAssetSum(custInfo.getAssetSum());
				custInfo.setDebtSum(custInfo.getDebtSum());
				custInfo.setRegistercapital(custInfo.getRegistercapital());
				custInfo.setMainBusiIncome(custInfo.getMainBusiIncome());
				custInfo.setOwnersEquity(custInfo.getOwnersEquity());
				
				model.addAttribute("custInfo",dispatchService.getCustomerInfo(custId));
				model.addAttribute("custAddressList",dispatchService.getCustomerAddress(custId));
				model.addAttribute("custManagerList",dispatchService.getCustomerManager(custId));
				model.addAttribute("custStockholderList",dispatchService.getCustomerStockholder(custId));
				forward="customer/CheckCustomer_Overview";	
			}
			
			//财务信息
			else if (type.toLowerCase().equals(DispatchTypeConstant.CUST_REPORT.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_ReportAbstract";
				}
				forward = "customer/AddCustomer_ReportAbstract";
			}
			
			//重大事项
			else if (type.toLowerCase().equals(DispatchTypeConstant.CUST_EVENTS.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_Events";
				}
				else{
					if(dispatchService.getCustomerEvents(custId) == null || dispatchService.getCustomerEvents(custId).size()==0){
						forward = "customer/AddCustomer_Events";
					}else{
						forward = "customer/CheckCustomer_Events";
						model.addAttribute("custEventsList",dispatchService.getCustomerEvents(custId));
					}	
				}
			}
			
			//我行
			else if (type.toLowerCase().equals(DispatchTypeConstant.CUST_INNER.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_InnerRelationship";
				}
				else{
					if(dispatchService.getCustomerDeposit(custId) == null || dispatchService.getCustomerDeposit(custId).size()==0 
							|| dispatchService.getCustomerLoan(custId) == null || dispatchService.getCustomerLoan(custId).size()==0 ){
						model.addAttribute("custDepositList", dispatchService.getCustomerDeposit(custId));
						model.addAttribute("custLoanList", dispatchService.getCustomerLoan(custId));
						forward = "customer/AddCustomer_InnerRelationship";
					}else{
						forward = "customer/CheckCustomer_InnerRelationship";
						model.addAttribute("custDepositList", dispatchService.getCustomerDeposit(custId));
						model.addAttribute("custLoanList", dispatchService.getCustomerLoan(custId));
					}	
				}
			}
			
			//他行
			else if (type.toLowerCase().equals(DispatchTypeConstant.CUST_OUTER.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_OuterRelationship";
				}
				else{
					if(dispatchService.getCustomerDeposit(custId) == null || dispatchService.getCustomerDeposit(custId).size()==0 
							|| dispatchService.getCustomerLoan(custId) == null || dispatchService.getCustomerLoan(custId).size()==0 ){
						forward = "customer/AddCustomer_OuterRelationship";
					}else{
						forward = "customer/CheckCustomer_OuterRelationship";
						model.addAttribute("custDepositList", dispatchService.getCustomerDeposit(custId));
						model.addAttribute("custLoanList", dispatchService.getCustomerLoan(custId));
					}	
				}
			}
			
			//担保信息
			else if(type.toLowerCase().equals(DispatchTypeConstant.CUST_GUARANTEE.toLowerCase())){
				if(custId==null || custId.equals("")){
					forward = "customer/AddCustomer_Guarantee";
				}
				else{
					if(dispatchService.getCustomerGuarantee(custId) == null || dispatchService.getCustomerGuarantee(custId).size()==0 ){
						forward = "customer/AddCustomer_Guarantee";
					}else{
						forward = "customer/CheckCustomer_Guarantee";
						model.addAttribute("custGuaranteeList", dispatchService.getCustomerGuarantee(custId));
					}	
				}
			}
			
			else{
				
			}

		}
		return forward;
	}

}
